from selenium.webdriver.common.by import By
from pages.base_page import Page


class BanListPage(Page):
    ele_title = (By.ID, u'page-bannedVisitor-title')

    banned_visitor_tab = (By.XPATH, u"//span[text()='Banned Visitors']")
    banned_ips_tab = (By.XPATH,  u"//span[text()='Banned IPs']")
    new_banned_visitor_button = (By.XPATH, u"//span[text()='New Banned Visitor']")
    new_banned_ip_button = (By.XPATH, u"//span[text()='New Banned IP']")
    new_banned_visitor_id_text = (By.NAME, u'visitorId')
    new_banned_visitor_note_text = (By.NAME, u'comment')
    new_banned_visitor_save_button = (By.XPATH, u"//span[text()='Save']")
    new_banned_ip_start_ip_text = (By.ID, u'form-bannedIp-input-ipRangeFrom')
    new_banned_ip_end_ip_text = (By.ID, u'form-bannedIp-input-ipRangeTo')
    new_banned_ip_note_text = (By.NAME, u'comment')
    new_banned_ip_save_button = (By.XPATH, u"//span[text()='Save']")

    delete_banned = (By.XPATH, u"//tr[@id='drag-0']/td[@data-tag='operation']/div/button[2]")
    confirm_delete = (By.XPATH, u"//span[text()='Delete']")

    # no feature
    ele_title_no_feature = (By.ID, u'page-bannedVisitor-title')
    ele_img = (By.XPATH, u"//img[@alt='Ban List Introduction']")
    ele_unlock_button = (By.XPATH, u"//span[text()='Unlock This Feature']")

    ele_error = (By.XPATH, '//div[@role="alert"]')

    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    def check_ban_list_page_when_has_ban_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.banned_ips_tab).is_enabled()
        self.find_element(*self.banned_visitor_tab).is_enabled()
        self.find_element(*self.new_banned_visitor_button).is_enabled()
        self.click(self.banned_ips_tab)
        self.find_element(*self.new_banned_ip_button).is_enabled()

    def check_add_banned_visitor_when_has_ban_feature(self):
        self.click(self.banned_visitor_tab)
        self.click(self.new_banned_visitor_button)
        self.input_text(self.new_banned_visitor_id_text, '1d3bb1e1-4c1d-45da-a0ec-903c29f4538d')
        self.input_text(self.new_banned_visitor_note_text, 'autotest')

    def check_save_add_banned_visitor(self):
        self.click(self.new_banned_visitor_save_button)

    def check_deleted_banned_visitor(self):
        self.click(self.delete_banned)
        self.click(self.confirm_delete)

    def check_add_banned_ips(self):
        self.click(self.banned_ips_tab)
        self.click(self.new_banned_ip_button)
        self.input_text(self.new_banned_ip_start_ip_text, '0.0.0.0')
        self.input_text(self.new_banned_ip_note_text, 'autotest')

    def check_save_add_banned_ips(self):
        self.click(self.new_banned_ip_save_button)

    def check_deleted_banned_ips(self):
        self.click(self.delete_banned)
        self.click(self.confirm_delete)

    def check_ban_list_page_when_no_ban_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_img).is_enabled()
        self.find_element(*self.ele_unlock_button).is_enabled()
